﻿using NHibernate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using WebSite.Core;
using WebSite.Data.NH.Infrastructure;
using WebSite.Model;

namespace WebSite.Data.NH.Repositories
{
    public interface IContactRepository : IRepository<ContactInfo>
    {
        /// <summary>
        /// 通过Id获取
        /// </summary>
        /// <param name="ContactId">Id</param>
        /// <returns>ContactInfo</returns>
        ContactInfo GetContact(int ContactId);

        /// <summary>
        /// 添加信息
        /// </summary>
        /// <param name="Contact">信息</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        bool AddContact(ContactInfo Contact);


        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="ContactId">Id</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        bool DeleteContact(int ContactId);


        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="Contact">信息</param>
        /// <returns>True=操作成功/False=操作失败</returns>
        bool UpdateContact(ContactInfo Contact);


        /// <summary>
        /// 根据条件分页获得记录
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="orderByExpressions">排序字段</param>
        /// <returns>记录列表</returns>
        IList<ContactInfo> LoadAll(Expression<Func<ContactInfo, bool>> where,
                                                   int pageIndex,
                                                   int pageSize,
                                                   params IOrderByExpression<ContactInfo>[] orderByExpressions);


        /// <summary>
        /// 获取总数
        /// </summary>
        /// <param name="where">条件</param>
        /// <returns>记录总数</returns>
        int LoadCount(Expression<Func<ContactInfo, bool>> where);
    }

    public class ContactRepository : NhRepositoryBase<ContactInfo>, IContactRepository
    {
        private readonly IUnitOfWork uow;

        public ContactRepository(ISession session, IUnitOfWork uow)
            : base(session)
        {
            this.uow = uow;
        }

        public ContactInfo GetContact(int ContactId)
        {
            var result = GetById(ContactId);
            return result;
        }

        public bool AddContact(ContactInfo Contact)
        {
            bool result = Add(Contact);
            uow.Commit();
            return result;
        }

        public bool DeleteContact(int ContactId)
        {
            var contact = GetById(ContactId);
            bool result = Delete(contact);
            uow.Commit();
            return result;
        }

        public bool UpdateContact(ContactInfo Contact)
        {
            bool result = Update(Contact);
            uow.Commit();
            return result;
        }

        public IList<ContactInfo> LoadAll(Expression<Func<ContactInfo, bool>> where, int pageIndex, int pageSize, params IOrderByExpression<ContactInfo>[] orderByExpressions)
        {
            var query = GetMany(where, pageIndex, pageSize, orderByExpressions);
            return query.ToList();
        }

        public int LoadCount(Expression<Func<ContactInfo, bool>> where)
        {
            var query = GetMany(where);
            return query.Count();
        }
    }
}
